<?php
namespace app\common\model;

use think\Cache;
use think\Db;
use think\Model;
use think\View;

class UserLike extends Model
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_like';
    
    /**
     * url   自动转化
     * @param $value
     * @return string
     */
    public function getUrlAttr($value)
    {
        $url = json_decode($value, true);
        if (!empty($url)) {
            $url = url($url['action'], $url['param'], true, true);
        } else {
            $url = '';
        }
        return $url;
    }

    /**
     * thumbnail 自动转化图片地址为绝对地址
     * @param $value
     * @return string
     */
    public function getThumbnailAttr($value)
    {
        if (!empty($value)) {
            $value = request()->domain().'/'.($value);
        }

        return $value;
    }

    /**
     * 点赞总数
     * @param $object_id
     * @param $type
     * @return int|string
     * @throws \think\Exception
     */
    public static function likeTotal($object_id, $type){
        return self::where(['object_id' => $object_id, 'table_name' =>$type])->count();
    }

}
